package com.ziqikeji.socket.clientThread;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端发送数据线程类
 * @author bcs
 */
public class Csend extends Thread{
    private Socket c;
    private String clientName;
    public Csend(Socket c,String clientName) {
        this.c = c;
        this.clientName = clientName;
    }
    @Override
    public void run() {
        try {
            //获取输出流
            OutputStreamWriter osw = new OutputStreamWriter(c.getOutputStream());
            BufferedWriter bw = new BufferedWriter(osw);
            //从键盘写入数据
            Scanner sc = new Scanner(System.in);
            while (true) {
                //将键盘写入的数据按行写入发送给服务器
                String msg = sc.nextLine() + "\n";
                bw.write(clientName + ":"+ msg);
                //刷新流
                bw.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
